<?php

class ApproveCourse extends MY_Controller {

    public function __construct() {
        parent::__construct();
        
        if($this->session->userdata('user_type') != 1 || $this->session->userdata('user_type') == null){
            show_error("You have no permission here");
        }
    }

    function index() {
        $this->load->library('pagination');
        $this->load->library('table');
        $this->load->Model('Course_Permission_Model');
//        $this->Course_Permission_Model->getAllCourseNotActive();

        $config['base_url'] = 'http://localhost/AsNew/ci/index.php/ApproveCourse/index';
        $config['total_rows'] = $this->Course_Permission_Model->getAllCourseNotActive()->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config);

        $query = $this->Course_Permission_Model->getAllCourseNotActive($config['per_page'], $this->uri->segment(3));
        $result = $query->result();

        $this->table->set_heading('No', 'Username', 'Course Code', 'Course Name', 'Hours', 'Prices', 'Status', 'Manage'); //Create columns

        if ($query->num_rows() > 0) {
            for ($i = 0; $i < count($result); $i++):
                $row = $result[$i];
                $this->table->add_row(
                        $i + 1, 
                        $row->USERNAME, 
                        $row->CODE, 
                        $row->NAME, 
                        $row->HOURS, 
                        $row->PRICE, 
                        $row->ACTIVE ? 'Active': 'In-Active', 
                        anchor(base_url() . 'index.php/ApproveCourse/Approve/' . $row->ID.'/'.$row->MEMBER_ID, '<img src="' . base_url() . 'images/approve_green_small.png" alt="Approve" />', array('onClick' => "return confirm('Are you sure want to Approve?')"))
                );
            endfor;
        }else {
            $cell = array('data' => 'No Record Found', 'colspan' => 8, 'align' => 'center', 'color' => 'red');
            $this->table->add_row($cell);
        }

        $tmpl = array(
            'table_open' => '<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">',
            'heading_cell_start' => '<th class="table-header-repeat line-left"><a href>',
            'heading_cell_end' => '</a></th>',
            'table_close' => '</table>');

        $this->table->set_template($tmpl);

        $this->data['main_content'] = 'approve_course';
        $this->load->view('includes/template', $this->data);
    }

    function searchCourse() {
        $this->load->library('pagination');
        $this->load->library('table');
        $this->load->Model('Course_Permission_Model');

        $search_code = $this->input->post('search_code');
        $search_name = $this->input->post('search_name');
        $search_user = $this->input->post('search_user');

        $config['base_url'] = 'http://localhost/AsNew/ci/index.php/ApproveCourse/index';
        $config['total_rows'] = $this->Course_Permission_Model->getAllCourseNotActive()->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config);


        $this->load->model('Course_Permission_Model');
        $query = $this->Course_Permission_Model->searchCourse($search_user, $search_code, $search_name, $config['per_page'], $this->uri->segment(3));
        $result = $query->result();


        $this->table->set_heading('No', 'Username', 'Course Code', 'Course Name', 'Hours', 'Prices', 'Status', 'Manage'); //Create columns

        if ($query->num_rows() > 0) {
            for ($i = 0; $i < count($result); $i++):
                $row = $result[$i];
                $this->table->add_row(
                        $i + 1, 
                        $row->USERNAME, 
                        $row->CODE, 
                        $row->NAME, 
                        $row->HOURS, 
                        $row->PRICE, 
                        $row->ACTIVE ? 'Active': 'In-Active', 
                        anchor(base_url() . 'index.php/ApproveCourse/Approve/' . $row->ID.'/'.$row->MEMBER_ID, '<img src="' . base_url() . 'images/approve_green_small.png" alt="Approve" />', array('onClick' => "return confirm('Are you sure want to Approve?')"))
                );
            endfor;
        }else {
            $cell = array('data' => 'No Record Found', 'colspan' => 8, 'align' => 'center', 'color' => 'red');
            $this->table->add_row($cell);
        }

        $tmpl = array(
            'table_open' => '<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">',
            'heading_cell_start' => '<th class="table-header-repeat line-left"><a href>',
            'heading_cell_end' => '</a></th>',
            'table_close' => '</table>');

        $this->table->set_template($tmpl);

        $this->data['main_content'] = 'approve_course';
        $this->load->view('includes/template', $this->data);
    }

    function Approve($courseID, $memberID){
        
        //change ACTIVE => True and insert menu permission for watch video
        $this->load->Model('Course_Permission_Model');
        $this->Course_Permission_Model->approveCourse($courseID, $memberID);

        redirect('ApproveCourse');
    }
    

}

?>
